/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.logist;


import dp1.titandevelop.titano.persistent.Distribuidor;
import dp1.titandevelop.titano.persistent.Proveedor;
import dp1.titandevelop.titano.service.ConexionService;
import dp1.titandevelop.titano.service.DistribuidorService;
import dp1.titandevelop.titano.service.ProveedorService;
import dp1.titandevelop.titano.service.ValidacionService;
import java.awt.Cursor;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Yulian
 */
public class ModulosT_LOG_ReporteCompras extends javax.swing.JInternalFrame {

    private List<Distribuidor> listaDist;
    private List<Proveedor> listaProv=new ArrayList<Proveedor>();
    public Integer idDistribuidor;

    private void addList() {

        for (int i = 0; i < listaProv.size(); i++) {
            this.cboxDistribuidor.addItem(listaProv.get(i).getRazonsocial());
        }
    }

    public ModulosT_LOG_ReporteCompras() {
        initComponents();
        ValidacionService val = new ValidacionService();
        val.NoEscribe(dpFechaFin);
        val.NoEscribe(dpFechaIn);
        DistribuidorService servAlm = new DistribuidorService();
        ProveedorService ps = new ProveedorService();
        this.listaProv = ps.buscarTodos();
        addList();
    }

    public int cboxIndex(JComboBox cbox) {
        return cbox.getSelectedIndex() - 1;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnReporte = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cboxDistribuidor = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        dpFechaIn = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        dpFechaFin = new org.jdesktop.swingx.JXDatePicker();

        setResizable(true);
        setTitle("Reporte de Compras");

        btnReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/reporte2.png"))); // NOI18N
        btnReporte.setToolTipText("Click para generar reporte");
        btnReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Proveedor");

        cboxDistribuidor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos" }));
        cboxDistribuidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxDistribuidorActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Rango"));

        jLabel6.setText("Fecha Mín:");

        jLabel7.setText("Fecha Máx:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dpFechaIn, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dpFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addGap(4, 4, 4))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dpFechaIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(dpFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cboxDistribuidor, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnReporte)
                .addGap(0, 0, 0)
                .addComponent(btnCancelar)
                .addGap(10, 10, 10))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboxDistribuidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnReporte, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(8, 8, 8))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteActionPerformed

        ValidacionService val = new ValidacionService();

                   if ((!val.esFechaVacia(dpFechaIn) && !val.esFechaVacia(dpFechaFin))
                           && !val.esCBoxVacio(cboxDistribuidor) || cboxIndex(cboxDistribuidor)==0 ) {

                ConexionService serv = new ConexionService();
                Connection con = serv.getConexion();
                try {
                  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                int indexAlm =cboxIndex(cboxDistribuidor), idAlm;
               
                Date in = this.dpFechaIn.getDate();
                Date fin = this.dpFechaFin.getDate();
                //String ubicacionReporte=System.getProperty("user.dir");
                InputStream archivoReporte = null;
                Map parametro = new HashMap();
                if(!val.esCBoxVacio(cboxDistribuidor) && !val.esFechaVacia(dpFechaIn)&& !val.esFechaVacia(dpFechaFin) ){//Caso en que se elige solo el parametro almacen
                    idAlm = this.listaProv.get(indexAlm).getIdproveedor();//
                    //ubicacionReporte +="\\src\\main\\java\\dp1\\titandevelop\\titano\\view\\Reportes\\Reporte-Venta.jasper";
                    archivoReporte = getClass().getResourceAsStream("/Reportes/Reporte_Compras.jasper");
                    
                    //ubicacionReporte = File.class.getResource("reportes/Reporte-Venta.jasper").toURI().getPath();
                    parametro.put("fechaIni",in);
                    parametro.put("fechaFin",fin);
                    parametro.put("proveedor",idAlm);
                }
                JasperReport jreport = (JasperReport) JRLoader.loadObject(archivoReporte);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jreport,parametro,con);
                JasperViewer jviewer = new JasperViewer(jasperPrint,false);  
                jviewer.setVisible(true); 
                this.setCursor(Cursor.getDefaultCursor());
                }catch(Exception e){    
                e.printStackTrace();
                JOptionPane.showInternalMessageDialog(this.getRootPane(),"Hubo un error en la generación del reporte","Error",JOptionPane.ERROR_MESSAGE);

            }
            } else {
                JOptionPane.showInternalMessageDialog(this.getRootPane(), "Por favor, ingrese las fechas correctas y un proveedor especifico", "Aviso", JOptionPane.WARNING_MESSAGE);
            }
        
    }//GEN-LAST:event_btnReporteActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void cboxDistribuidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxDistribuidorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboxDistribuidorActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnReporte;
    private javax.swing.JComboBox cboxDistribuidor;
    private org.jdesktop.swingx.JXDatePicker dpFechaFin;
    private org.jdesktop.swingx.JXDatePicker dpFechaIn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
